package immoc_collection.map.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 备选课程类
 *
 *  增 删  改  遍历 查?
 */
public class ListTest {

    /**
     * 存放备选课程的List
     */
    public List courseToSelect;

    public ListTest() {
        this.courseToSelect = new ArrayList();
    }

    /**
     * 用于往courseToSelect添加课程
     */
    public void testAdd() {
        // 创建一个课程对象
        Course cr1 = new Course("1", "数据结构");
        // 添加入课程List中
        courseToSelect.add(cr1);

        // 获取课程, 集合中存的Object, 需要强行转换为Course
        Course temp = (Course) courseToSelect.get(0);
        System.out.println("添加了课程 : " + temp.name);

        // 指定的位置插入元素
        Course cr2 = new Course("2", "C语言");
        courseToSelect.add(0, cr2);
        // 获取课程, 集合中存的Object, 需要强行转换为Course
        Course temp2 = (Course) courseToSelect.get(0);
        System.out.println("添加了课程 : " + temp2.name);

        // 指定的位置 超过长度, 或者小于0
//        Course cr3 = new Course("3", "test");
//        courseToSelect.add(3, cr3); // throw IndexOutOfBoundsException越界异常

        Course[] courses = {new Course("3", "离散数学"), new Course("4", "汇编语言")};
//        courseToSelect.addAll(courses); // 需要将数组转为List
        courseToSelect.addAll(Arrays.asList(courses));
        Course cr3 = (Course) courseToSelect.get(2);
        Course cr4 = (Course) courseToSelect.get(3);
        System.out.println("添加了课程 : " + cr3.name);
        System.out.println("添加了课程 : " + cr4.name);

        Course[] course2 = {new Course("5", "高等数学"), new Course("6", "大学英语")};
        courseToSelect.addAll(2, Arrays.asList(course2));
        Course cr5 = (Course) courseToSelect.get(2);
        Course cr6 = (Course) courseToSelect.get(3);
        System.out.println("添加了课程 : " + cr5.name);
        System.out.println("添加了课程 : " + cr6.name);

        // 添加重复数据
        courseToSelect.add(cr1);
    }

    public void testGet() {
        System.out.println("通过for循环输出课程");
        int size = courseToSelect.size();
        for (int i = 0; i < size; i++) {
            Course temp = (Course) courseToSelect.get(i);
            System.out.println("课程: id " + temp.id + "  : " + temp.name);
        }
    }

    public void testIterator() {
        System.out.println("通过迭代器循环输出课程");
        Iterator it = courseToSelect.iterator();
        while (it.hasNext()) {
            Course temp = (Course) it.next();
            System.out.println("课程: id " + temp.id + "  : " + temp.name);
        }
    }

    public void testForEach() {
        System.out.println("通过ForEach循环输出课程");
        for (Object obj : courseToSelect) {
            Course temp = (Course) obj;
            System.out.println("课程: id " + temp.id + "  : " + temp.name);
        }
    }

    public void testModify() {
        courseToSelect.set(4, new Course("7", "毛概"));
        testForEach();
    }

    public void testRemove() {
        System.out.println("移除对象");
        Course temp = (Course) courseToSelect.get(4);
        System.out.println("课程: id " + temp.id + "  : " + temp.name);
//        courseToSelect.remove(4); // 通过索引下标
        courseToSelect.remove(temp); // 通过对象
        System.out.println("删除成功!");
        testForEach();
    }

    // 删除多个元素
    public void testRemoveAll() {
        Course[] courses = {(Course) courseToSelect.get(3), (Course) courseToSelect.get(4)};
        courseToSelect.removeAll(Arrays.asList(courses));
        // 同时删除多个成功
        System.out.println("同时删除多个成功");
        testForEach();
    }

    /**
     * 添加一些奇怪 东西
     */
    public void testAddDiffType() {
        String s = "我不是课程";
        // Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to immoc_collection.map.demo.Course
        courseToSelect.add(s);
        testForEach();
    }

    /**
     * 测试是否存在
     */
    public void testContain() {

        // 1. 重新创建一个相同属性的对象, 显示不存在
        // 2. Override 类Course的equals的方法, 就可以判断出是存在的
        Course c = new Course("2", "C语言");
//        Course c = (Course) courseToSelect.get(1); // 获取原来的元素, 存在

        Boolean rst = courseToSelect.contains(c);
        if (rst) {
            System.out.println("包含课程: " + c.name);
        } else {
            System.out.println("查找的课程不存在");
        }
    }

    public static void main(String[] args) {
        ListTest lt = new ListTest();
        // 添加
        lt.testAdd();

        // 获取
        lt.testGet();
//        lt.testIterator();
//        lt.testForEach();

        // 修改
//        lt.testModify();

        // 删除
//        lt.testRemove();
//        lt.testRemoveAll();

        // 添加不同类型的元素
//        lt.testAddDiffType();

        // 是否存在
        lt.testContain();
    }
}
